<template>
  <div class="service-run-monitor p-4">
    <!-- 搜索区域 -->
    <div class="search-area mb-4 bg-white p-4 rounded-lg shadow-md">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择" clearable>
            <el-option label="成功" value="success" />
            <el-option label="失败" value="fail" />
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <div class="i-carbon-search mr-1"></div>查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 监控按钮 -->
    <div class="mb-4">
      <el-button type="success" @click="handleMonitor">
        <div class="i-carbon-monitor mr-1"></div>监控
      </el-button>
    </div>

    <!-- 服务列表 -->
    <div class="service-list bg-white p-4 rounded-lg shadow-md">
      <ArtTable :data="serviceList" :loading="loading" border stripe>
        <el-table-column prop="serviceName" label="服务名称" />
        <el-table-column prop="callUser" label="调用用户" />
        <el-table-column prop="callIp" label="调用IP" />
        <el-table-column prop="callParams" label="调用参数" />
        <el-table-column prop="callResult" label="调用结果">
          <template #default="scope">
            <el-tag :type="scope.row.callResult === '成功' ? 'success' : 'danger'">
              {{ scope.row.callResult }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="callTime" label="记录日期" />
      </ArtTable>

      <!-- 分页 -->
      <div class="mt-4 flex justify-end">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 监控弹窗 -->
    <el-dialog v-model="monitorDialogVisible" title="监控" width="500px">
      <div class="monitor-content">
        <div class="flex items-center justify-between mb-4">
          <div class="text-lg">调用结果统计</div>
          <div class="flex items-center">
            <div class="flex items-center mr-4">
              <div class="w-3 h-3 rounded-full bg-success mr-2"></div>
              <span>成功</span>
            </div>
            <div class="flex items-center">
              <div class="w-3 h-3 rounded-full bg-danger mr-2"></div>
              <span>失败</span>
            </div>
          </div>
        </div>
        <ArtRingChart
          :percentage="80"
          :color="['#67C23A', '#F56C6C']"
          :data="[
            { name: '成功', value: 80 },
            { name: '失败', value: 20 }
          ]"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue'

  // 搜索表单
  const searchForm = reactive({
    status: '',
    dateRange: []
  })

  // 服务列表数据
  const loading = ref(false)
  const currentPage = ref(1)
  const pageSize = ref(10)
  const total = ref(5)

  const serviceList = ref([
    {
      serviceName: '学生数据子类',
      callUser: '教务',
      callIp: '192.168.1.20',
      callParams: "XBM = '1' and ZYMC = '信息技术'",
      callResult: '成功',
      callTime: '2023-05-24 14:47:19'
    },
    {
      serviceName: '学生数据子类',
      callUser: '教务',
      callIp: '192.168.1.20',
      callParams: "XBM = '1page=1",
      callResult: '失败',
      callTime: '2023-05-24 14:29:16'
    },
    {
      serviceName: 'Test01',
      callUser: '',
      callIp: '218.4.170.236',
      callParams: '',
      callResult: '成功',
      callTime: '2019-11-27 09:12:53'
    },
    {
      serviceName: '校公文服务未授权目录参数',
      callUser: '',
      callIp: '0.0.0.0.0.0.0.1',
      callParams: "GWBH = 'GX01'",
      callResult: '成功',
      callTime: '2019-11-15 11:37:22'
    },
    {
      serviceName: '校公文服务',
      callUser: '',
      callIp: '0.0.0.0.0.0.0.1',
      callParams: '',
      callResult: '成功',
      callTime: '2019-11-15 11:32:20'
    }
  ])

  // 监控弹窗
  const monitorDialogVisible = ref(false)

  // 搜索方法
  const handleSearch = () => {
    loading.value = true
    // TODO: 实现搜索逻辑
    setTimeout(() => {
      loading.value = false
    }, 1000)
  }

  // 监控按钮点击
  const handleMonitor = () => {
    monitorDialogVisible.value = true
  }

  // 分页方法
  const handleSizeChange = (val: number) => {
    pageSize.value = val
    handleSearch()
  }

  const handleCurrentChange = (val: number) => {
    currentPage.value = val
    handleSearch()
  }
</script>

<style scoped lang="scss">
  .service-run-monitor {
    .monitor-content {
      padding: 20px;
    }
  }
</style>
